const express = require("express");
const router = express.Router();

const db = require("../db");

//获取用户信息
router.get("/userinfo", async (req, res) => {
  const result = await db(
    `select id,username,nickname,email,headUrl,position,company,introduce from user where id=${req.user.id}`
  );
  res.send({ status: 0, message: "获取用户信息成功", data: result[0] });
});

//更新用户信息
router.put("/userinfo", async (req, res) => {
  const { nickname, email, headUrl, position, company, introduce } = req.body;
  await db(
    `update user set nickname='${nickname}',email='${email}',headUrl='${headUrl}',position='${position}',company='${company}',introduce='${introduce}' where id=${req.user.id}`
  );
  res.send({ status: 0, message: "修改信息成功" });
});

//获取用户列表
router.get("/user-list", async (req, res) => {
  const result = await db(
    `select id,nickname,headUrl,email,position,company,introduce from user`
  );
  res.send({ status: 0, message: "获取用户列表成功", data: result });
});

// router.get("/student", function (req, res) {
//   db(`select * from student`, (err, result) => {
//     if (err) throw err;
//     res.send({ status: 0, message: "获取成功", data: result });
//   });
// });

// router.delete("/student", function (req, res) {
//   const { id } = req.query;
//   db(`delete from student where id=${id}`, (err, result) => {
//     if (err) throw err;
//     res.send({ status: 0, message: "删除成功" });
//   });
// });

// router.post("/student", function (req, res) {
//   console.log(req.body);
//   const { username, age, sex } = req.body;
//   db(
//     `insert into student set username='${username}',age=${age},sex='${sex}'`,
//     (err, result) => {
//       if (err) throw err;
//       res.send({ status: 0, message: "新增成功" });
//     }
//   );
// });

// router.put("/student", function (req, res) {
//   const { id, username, age, sex } = req.body;
//   db(
//     `update student set username='${username}',age=${age},sex='${sex}' where id=${id}`,
//     (err, result) => {
//       if (err) throw err;
//       res.send({ status: 0, message: "修改成功" });
//     }
//   );
// });

module.exports = router;
